package com.example.chinesechess.presentation.ui.screen

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.example.chinesechess.R
import com.example.chinesechess.domain.model.Side
import com.example.chinesechess.presentation.ui.component.ChessBoard
import com.example.chinesechess.presentation.viewmodel.GameState
import com.example.chinesechess.presentation.viewmodel.GameViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GameScreen(
    navController: NavController, 
    gameMode: String,
    viewModel: GameViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsState()
    val gameState by viewModel.gameState.collectAsState()
    
    // 初始化游戏模式
    LaunchedEffect(key1 = gameMode) {
        viewModel.startNewGame(gameMode)
    }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { 
                    Text(
                        text = when (gameMode) {
                            "local" -> stringResource(R.string.mode_local_game)
                            "ai_easy" -> stringResource(R.string.mode_ai_easy)
                            "ai_medium" -> stringResource(R.string.mode_ai_medium)
                            "ai_hard" -> stringResource(R.string.mode_ai_hard)
                            else -> stringResource(R.string.app_name)
                        }
                    ) 
                },
                navigationIcon = {
                    IconButton(onClick = { navController.popBackStack() }) {
                        // Replace with actual back icon
                        Text("<")
                    }
                }
            )
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // Game status indicator
            Surface(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                color = when {
                    gameState is GameState.GameOver -> MaterialTheme.colorScheme.errorContainer
                    uiState.isInCheck -> MaterialTheme.colorScheme.tertiaryContainer
                    else -> MaterialTheme.colorScheme.primaryContainer
                },
                shape = MaterialTheme.shapes.medium
            ) {
                Text(
                    text = when {
                        gameState is GameState.GameOver -> {
                            val winner = (gameState as GameState.GameOver).winner
                            if (winner == null) {
                                stringResource(R.string.status_draw)
                            } else {
                                val winnerText = if (winner == Side.RED) "红方" else "黑方"
                                "$winnerText 胜利!"
                            }
                        }
                        uiState.isInCheck -> stringResource(R.string.status_check)
                        else -> {
                            val sideText = if (uiState.currentSide == Side.RED) "红方" else "黑方"
                            "轮到${sideText}行棋"
                        }
                    },
                    modifier = Modifier.padding(16.dp),
                    textAlign = TextAlign.Center,
                    style = MaterialTheme.typography.titleMedium
                )
            }
            
            // Chess board
            ChessBoard(
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(9f/10f)
                    .padding(16.dp),
                viewModel = viewModel
            )
            
            // Game controls
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                OutlinedButton(
                    onClick = { viewModel.undoMove() },
                    enabled = gameState is GameState.Playing
                ) {
                    Text(stringResource(R.string.game_undo))
                }
                
                OutlinedButton(
                    onClick = { /* Offer draw */ },
                    enabled = gameState is GameState.Playing
                ) {
                    Text(stringResource(R.string.game_draw_offer))
                }
                
                OutlinedButton(
                    onClick = { viewModel.resign() },
                    enabled = gameState is GameState.Playing
                ) {
                    Text(stringResource(R.string.game_resign))
                }
            }
            
            // New game button (shows when game is over)
            if (gameState is GameState.GameOver) {
                Button(
                    onClick = { viewModel.startNewGame() },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp, vertical = 8.dp)
                ) {
                    Text(stringResource(R.string.dialog_rematch))
                }
            }
        }
    }
} 